package com.ruoyi.course.service.impl;

import java.util.List;

import com.ruoyi.common.constant.SysConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.course.domain.SysPaperTopic;
import com.ruoyi.course.mapper.SysPaperTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.SysPaperMapper;
import com.ruoyi.course.domain.SysPaper;
import com.ruoyi.course.service.ISysPaperService;

/**
 * 试卷Service业务层处理
 * 
 * @author YJF
 */
@Service
public class SysPaperServiceImpl implements ISysPaperService 
{
    @Autowired
    private SysPaperMapper sysPaperMapper;

    @Autowired
    private SysPaperTopicMapper sysPaperTopicMapper;

    /**
     * 查询试卷
     * 
     * @param sysPaper 试卷
     * @return 试卷
     */
    @Override
    public SysPaper selectSysPaperByPaperId(SysPaper sysPaper)
    {
        return sysPaperMapper.selectSysPaperByPaperId(sysPaper);
    }

    /**
     * 查询试卷列表
     * 
     * @param sysPaper 试卷
     * @return 试卷
     */
    @Override
    public List<SysPaper> selectSysPaperList(SysPaper sysPaper)
    {
        if(StringUtils.isNull( sysPaper.getDelFlag())){
            sysPaper.setDelFlag(SysConstants.CONSTANT_STR0);
        }
        return sysPaperMapper.selectSysPaperList(sysPaper);
    }

    /**
     * 新增试卷
     * 
     * @param sysPaper 试卷
     * @return 结果
     */
    @Override
    public int insertSysPaper(SysPaper sysPaper)
    {
        //创建者ID
        sysPaper.setUserId(SecurityUtils.getUserId());
        sysPaper.setCreateTime(DateUtils.getNowDate());
        return sysPaperMapper.insertSysPaper(sysPaper);
    }

    /**
     * 修改试卷
     * 
     * @param sysPaper 试卷
     * @return 结果
     */
    @Override
    public int updateSysPaper(SysPaper sysPaper)
    {
        //统计试题总数
        SysPaperTopic paperTopic = new SysPaperTopic();
        paperTopic.setPaperId(sysPaper.getPaperId());   //试卷ID
        paperTopic.setTopicStatus(SysConstants.CONSTANT_INT0);    //题目状态
        paperTopic.setDelFlag(SysConstants.CONSTANT_STR0);
        //题目数量
        int topicNum = sysPaperTopicMapper.topicSum(paperTopic);

        sysPaper.setTopicNum(topicNum);
        sysPaper.setUpdateTime(DateUtils.getNowDate());
        return sysPaperMapper.updateSysPaper(sysPaper);
    }

    /**
     * 批量删除试卷
     * 
     * @param paperIds 需要删除的试卷主键
     * @return 结果
     */
    @Override
    public int deleteSysPaperByPaperIds(Long[] paperIds)
    {
        return sysPaperMapper.deleteSysPaperByPaperIds(paperIds);
    }

    /**
     * 删除试卷信息
     * 
     * @param paperId 试卷主键
     * @return 结果
     */
    @Override
    public int deleteSysPaperByPaperId(Long paperId)
    {
        return sysPaperMapper.deleteSysPaperByPaperId(paperId);
    }

    /**
     * 校验试卷名称是否唯一
     * @param sysPaper 试卷
     * @return 结果
     */
    @Override
    public String checkPaperNameUnique(SysPaper sysPaper)
    {
        sysPaper.setDelFlag(SysConstants.CONSTANT_STR0);
        int count = sysPaperMapper.checkPaperNameUnique(sysPaper);
        if (count > 0)
        {
            return SysConstants.CONSTANT_STR1;
        }
        return SysConstants.CONSTANT_STR0;
    }
}
